package com.jason.kotlin.gank.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.jason.kotlin.gank.model.*
import com.jason.kotlin.gank.repository.CaiYunApiImpl
import com.jason.kotlin.gank.repository.RollApiImpl
import com.jason.kotlin.gank.repository.ZhiXinApiImpl
import com.jason.library.common.base.BaseViewModel
import kotlinx.coroutines.launch
import java.lang.Exception

/**
 * @description: RollPhoneAddressViewModel
 *
 * @author ChenBaoyang
 * @date 4/23/21 14:33
 */
class RollMoreViewModel(application: Application) : BaseViewModel(application) {

    val dataPhone = MutableLiveData<RollMobileBean>()

    val dataIp = MutableLiveData<RollIpBean>()

    val dataPhoneCodes = MutableLiveData<List<RollPhoneBean>>()

    val dataHistoryToday = MutableLiveData<List<RollHistoryBean>>()

    val dataRubbish = MutableLiveData<RollRubbishBean>()

    val dataDictionary = MutableLiveData<RollDictionaryBean>()

    val dataQRCode = MutableLiveData<RollQRCodeBean>()



    private val rollApi = RollApiImpl()



    fun getMobileLocation(mobile: String) {
        viewModelScope.launch{
            showLoading()
            try {
                val response = rollApi.getMobileLocation(mobile)
                dataPhone.postValue(response.data)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getIpLocationSelf() {
        viewModelScope.launch{
            showLoading()
            try {
                val response = rollApi.getIpLocationSelf()
                dataIp.postValue(response.data)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getIpLocation(ip: String) {
        viewModelScope.launch{
            showLoading()
            try {
                val response = rollApi.getIpLocation(ip)
                dataIp.postValue(response.data)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getWorldPhoneCode() {
        viewModelScope.launch {
            showLoading()
            try {
                val response = rollApi.getWorldPhoneCode()
                if(response.data.isNotEmpty()) {
                    dataPhoneCodes.postValue(response.data)
                }

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getHistoryToday() {
        viewModelScope.launch {
            showLoading()
            try {
                val response = rollApi.getHistoryToday()
                dataHistoryToday.postValue(response.data)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getRubbishType(name: String) {
        viewModelScope.launch {
            showLoading()
            try {
                val response = rollApi.getRubbishType(name)
                dataRubbish.postValue(response.data)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun convertDictionary(word: String) {
        viewModelScope.launch {
            showLoading()
            try {
                val response = rollApi.convertDictionary(word)
                dataDictionary.postValue(response.data[0])

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getQRCode(content: String) {
        viewModelScope.launch {
            showLoading()
            try {
                val response = rollApi.getQRCode(content)
                dataQRCode.postValue(response.data)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

}